package cn.zhuama.org.controller;

import cn.zhuama.basic.util.AjaxResult;
import cn.zhuama.basic.util.PageList;
import cn.zhuama.org.domain.Employee;
import cn.zhuama.org.query.EmployeeQuery;
import cn.zhuama.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Employee finOne( @ApiParam(name="编号",required = true) @PathVariable("id") Long id){
        return employeeService.londOne(id);
    }
    @GetMapping
    public List<Employee> finAll(){
        return employeeService.loadAll();
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            employeeService.removeById(id);
            return AjaxResult.me().setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败:"+e.getMessage());
        }
    }
    @PutMapping    //{success:true,messasge:编辑成功,data:}
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){
                employeeService.save(employee);
            }else{
                employeeService.editById(employee);
            }
            return AjaxResult.me().setMessage("编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("编辑失败:"+e.getMessage());
        }
    }
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            employeeService.patchDel(ids);
            return AjaxResult.me().setMessage("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败");
        }
    }
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        PageList<Employee> page = employeeService.queryData(employeeQuery);
        return page;
    }
}
